package com.xiaoyu.awakening.common.core.kit;

/**
 * 数字转换工具类
 *
 * @author wangxiaoyu
 * @since v1.0
 */
public class NumberConvertKit {
    /**
     * 私有化构造函数
     */
    private NumberConvertKit() {
    }

    /**
     * 中文数字
     */
    private static final String[] NUM_CN_ARRAY = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};

    /**
     * 中文单位
     */
    private static final String[] NUM_CN_UNIT_ARRAY = {"", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千"};

    /**
     * 阿拉伯数字转换为汉字格式
     *
     * @param number 阿拉伯正整数、0
     * @author wangxiaoyu
     * @since v1.0
     */
    public static String number2CnNumber(int number) {
        if (number == 0) {
            return NUM_CN_ARRAY[0];
        }
        StringBuilder cnNumber = new StringBuilder();
        int count = 0;
        while (number > 0) {
            cnNumber.insert(0, (NUM_CN_ARRAY[number % 10] + NUM_CN_UNIT_ARRAY[count]));
            number = number / 10;
            count++;
        }
        String result = cnNumber.toString().replaceAll("零[千百十]", "零").replaceAll("零+万", "万").replaceAll("零+亿", "亿").replaceAll("亿万", "亿零").replaceAll("零+", "零").replaceAll("零$", "");
        if (result.startsWith("一十")) {
            result = result.replace("一十", "十");
        }
        return result;
    }
}
